<?php
namespace module\tv\controller;

use think\Request;
use think\Config;
use core\tv\model\AdverModel;
use core\tv\logic\AdverLogic;
use core\tv\validate\AdverValidate;

class Adver extends Base
{

    /**
     * 广告列表
     * 
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $this->siteTitle = '广告列表';

        $map = [];

        // 查询条件-关键词
        $keyword = $request->param('keyword');
        if ($keyword != '') {
            $map['adver_name'] = [
                'like',
                '%' . $keyword . '%'
            ];
        }
        $this->assign('keyword', $keyword);

        //列表
        $model = AdverModel::getInstance()->where($map)->order('id DESC');
        $this->_page($model);

        //状态选择
        $adverStatus = AdverLogic::getInstance()->getAdverStatus();
        $this->assign('adverStatus', $adverStatus);

        //广告位选择
        $adverPosition = Config::get('tv_adver_position');
        $this->assign('adverPosition', $adverPosition);

        return $this->fetch();
    }

    /**
     * 添加广告
     * 
     * @param Request $request
     * @return mixed
     */
    public function add(Request $request)
    {
        if($request->isPost()){
            $data = [
                'adver_name' => $request->param('adver_name'),
                'position' => $request->param('position'),
                'src' => $request->param('src'),
                'link' => $request->param('link'),
                'status' => $request->param('status')
            ];

            //验证
            $this->_validate(AdverValidate::class, $data, 'add');

            //添加
            $this->_add(AdverModel::class, $data);
        }else{
            $this->siteTitle = "添加广告";

            //广告位选择
            $adverPosition = Config::get('tv_adver_position');
            $this->assign('adverPosition', $adverPosition);

            //状态选择
            $adverStatus = AdverLogic::getInstance()->getAdverStatus();
            $this->assign('adverStatus', $adverStatus);

            return $this->fetch();
        }
    }

    /**
     * 编辑广告
     * 
     * @param Request $request
     * @return mixed
     */
    public function edit(Request $request)
    {
        if($request->isPost()){
            $data = [
                'adver_name' => $request->param('adver_name'),
                'position' => $request->param('position'),
                'src' => $request->param('src'),
                'link' => $request->param('link'),
                'status' => $request->param('status')
            ];

            //验证
            $this->_validate(AdverValidate::class, $data, 'add');

            //添加
            $this->_edit(AdverModel::class, $data);
        }else{
            $this->siteTitle = "编辑广告";

            //记录    
            $this->_record(AdverModel::class);

            //广告位选择
            $adverPosition = Config::get('tv_adver_position');
            $this->assign('adverPosition', $adverPosition);

            //状态选择
            $adverStatus = AdverLogic::getInstance()->getAdverStatus();
            $this->assign('adverStatus', $adverStatus);

            return $this->fetch();
        }
    }

    /**
     * 修改字段值
     *
     * @param Request $request            
     * @return mixed
     */
    public function modify(Request $request)
    {
        $fields = [
            'status',
            'position'
        ];
        $this->_modify(AdverModel::class, $fields);
    }

    /**
     * 删除广告
     * 
     * @param Request $request
     * @return void
     */
    public function delete(Request $request)
    {
        $this->_delete(AdverModel::class, false);
    }
}